//
//  BlurBackgroundView.m
//  Sight
//
//  Created by chen on 16/6/10.
//  Copyright (c) 2016年 chen. All rights reserved.
//

#import "BlurBackground.h"
#import <GPUImage/GPUImage.h>

@interface BlurBackground ()

@property (strong,nonatomic) GPUImageiOSBlurFilter *filter;
@property (strong,nonatomic) UIImageView *blurImageView;
@end


@implementation BlurBackground

+(BlurBackground *)sharedBlurground
{
    static BlurBackground *shareBlurground;
    static dispatch_once_t predicate;
    dispatch_once(&predicate,^{
        shareBlurground=[[self alloc]init];
        shareBlurground.filter=[[GPUImageiOSBlurFilter alloc]init];
        shareBlurground.filter.blurRadiusInPixels=1.0f;
        shareBlurground.blurImageView=[[UIImageView alloc]init];
        [shareBlurground.blurImageView setUserInteractionEnabled:YES];
        UITapGestureRecognizer *gesture=[[UITapGestureRecognizer alloc]initWithTarget:shareBlurground action:@selector(imgaeViewTapped:)];
        [shareBlurground.blurImageView addGestureRecognizer:gesture];
    });
    return shareBlurground;
}

-(UIView *)addBlurBackgroundToView:(UIView *)backgroundView
{
    
    UIGraphicsBeginImageContext(backgroundView.bounds.size);
    [backgroundView drawViewHierarchyInRect:backgroundView.bounds afterScreenUpdates:YES];
    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.blurImageView.frame=backgroundView.frame;
    self.blurImageView.image=[self.filter imageByFilteringImage:image];
    [backgroundView addSubview:self.blurImageView];
    return self.blurImageView;
}


-(void)removeBackgroundFromView
{
    
    [self.blurImageView removeFromSuperview];
}

-(void)imgaeViewTapped:(UITapGestureRecognizer *)gesture
{
    [self removeBackgroundFromView];
}
@end
